/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.composite.command;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.uml2.uml.ConnectableElement;
import org.eclipse.uml2.uml.Connector;
import org.eclipse.uml2.uml.ConnectorEnd;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.EncapsulatedClassifier;
import org.eclipse.uml2.uml.Port;
import org.eclipse.uml2.uml.Property;

import com.cea.papyrus.core.commands.connection.ConnectionCreateCommand;
import com.cea.papyrus.core.commands.connection.IConnectionCreateCommand;
import com.cea.papyrus.core.pseudoelement.UMLElementFactory;
import com.cea.papyrus.diagram.composite.helper.EdgeConnectorHelper;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * 
 */
public class ConnectorCreateCommand extends ConnectionCreateCommand implements
IConnectionCreateCommand {

	/**
	 * 
	 * 
	 * @param type 
	 * @param location 
	 * @param source 
	 */
	public ConnectorCreateCommand(GraphElement source, Object type, Point location) {
		super(source, type, location);
		// TODO Auto-generated constructor stub
	}
	public boolean canExecute() {
		Boolean b=super.canExecute();
		if (!b){return false;}
		if (getSource().getContainer() instanceof Diagram 
				&& getTarget().getContainer() instanceof Diagram		
		){
			return false;
			
		}
		if (getSource().getContainer() instanceof Diagram 
				&& !(getTarget().getContainer() instanceof Diagram)		
		){
			if(getSource().getSemanticParent().equals(getTarget().getContainer())){ 
			return true;
			}
			return false;
		}
		if ((!(getSource().getContainer() instanceof Diagram)) 
				&& (getTarget().getContainer() instanceof Diagram)		
		){
			if(getTarget().getSemanticParent().equals(getSource().getContainer())){ 
			return true;
			}
			return false;
		}
	return true;
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public Element createBussinessModel() {
		Connector umlLink=null;
		
		//look for the parentElement
		GraphElement ge=((GraphElement)getSource());
		setOwner((GraphElement)getSource().getContainer());
		Element elte = ((Uml1SemanticModelBridge)ge.getSemanticModel()).getElement().getOwner();
		EdgeConnectorHelper ech= new EdgeConnectorHelper();
		umlLink=(Connector)ech.createUMLElement(elte);

		//connect the graphEdge to the diagram and UML Elements
		Element umlSource  = getSourceElement();
		Element umlTarget  = getTargetElement();

		//((Notifier)getSource()).eAdapters().clear();
		//((Notifier)getTarget()).eAdapters().clear();
		//((Notifier)getOwner()).eAdapters().clear();


		if(type.equals(org.eclipse.uml2.uml.Connector.class)) {
			umlLink.setName(((ConnectableElement)umlSource).getName()+((ConnectableElement)umlTarget).getName()+"connector");
			((ConnectorEnd)umlLink.getEnds().get(0)).setRole((ConnectableElement)umlSource);
			((ConnectorEnd)umlLink.getEnds().get(1)).setRole((ConnectableElement)umlTarget);

			// setuml umlparent
			if(umlSource instanceof Port){
				setOwner((GraphElement)getSource().getSemanticParent());
				Element elt=((Uml1SemanticModelBridge)((GraphNode)getSource().getSemanticParent()).getSemanticModel()).getElement();

				if (elt instanceof Property){
					((EncapsulatedClassifier)((Property)elt).getOwner()).getOwnedConnectors().add(umlLink);}	
				else{//classifier
					((EncapsulatedClassifier)elt).getOwnedConnectors().add(umlLink);
				}
			}
			//else{
				//((EncapsulatedClassifier)((Property)umlSource).getOwner()).getOwnedConnectors().add(umlLink);
			//	}

			//finish conector end
			if(umlSource instanceof Port){

				Element elt=((Uml1SemanticModelBridge)((GraphNode)getSource().getSemanticParent()).getSemanticModel()).getElement();
				if (elt instanceof Property) {
					((ConnectorEnd)umlLink.getEnds().get(0)).setPartWithPort((Property)elt);
				}
			}

			if(umlTarget instanceof Port){
				Element elt=((Uml1SemanticModelBridge)((GraphNode)getTarget().getSemanticParent()).getSemanticModel()).getElement();
				if (elt instanceof Property) {
					((ConnectorEnd)umlLink.getEnds().get(1)).setPartWithPort((Property)elt);
				}
			}

		}

		//connect UML element
		//ModelerHelper.getFactory().connect(type,umlSource, umlTarget,getEdgeElement());
		// Once the connection is created
		// set Stetreotypes and Presentation
		// Add stereotype and properties if needed...
		
			if ((enhancedType != null) 
					&& (enhancedType instanceof UMLElementFactory)
					&& (((UMLElementFactory) enhancedType).getStereotypes().size() > 0)) {
				//. setStereotype
				setStereotypes(umlLink);		
				
			
		}

		// Return the created bussiness element. It is stored inside the GraphEdge.
		return umlLink;
//		return getBussinessElementFromGraphElement(getEdge());
	}
	
}
